package com.yupi.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.bcel.internal.generic.LUSHR;
import com.sun.org.apache.xpath.internal.operations.Bool;
import com.yupi.project.common.BaseResponse;
import com.yupi.project.common.ErrorCode;
import com.yupi.project.common.ResultUtils;
import com.yupi.project.exception.BusinessException;
import com.yupi.project.model.dto.product.ProductAddRequest;
import com.yupi.project.model.dto.product.ProductQueryRequest;
import com.yupi.project.model.dto.product.ProductUpdateRequest;
import com.yupi.project.model.entity.Product;
import com.yupi.project.model.entity.Product;
import com.yupi.project.model.entity.Resourse;
import com.yupi.project.model.vo.ProductVo;
import com.yupi.project.service.ProductService;
import com.yupi.project.service.ResourseService;
import com.yupi.project.util.FileUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("product")
@CrossOrigin
public class ProductController {
    @Resource
    private ProductService productService;


    @Resource
    private ResourseService resourseService;

    @Resource
    private FileUtil fileUtil;

    /**
     * 增加产品
     *
     * @return
     */
    @PostMapping("")
    public BaseResponse<Long> addProduct(ProductAddRequest productAddRequest) {

        if (productAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (!StringUtils.isNotBlank(productAddRequest.getName())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "产品名称不能为空");
        }


//
        Long id = productService.saveProduct(productAddRequest);

        return ResultUtils.success(id);


    }

    /**
     * 修改产品
     *
     * @return
     */
    @PutMapping("")
    public BaseResponse<Long> updateProduct(ProductUpdateRequest productUpdateRequest) {

        if (productUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (productUpdateRequest.getId() == 0 || productUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "该产品不存在");
        }


//
        Long id = productService.updateProduct(productUpdateRequest);

        return ResultUtils.success(id);

    }


    /**
     * 删除产品
     *
     * @return
     */
    @DeleteMapping("")
    public BaseResponse<Boolean> deleteProduct(Long id) {

        if (id == null || id == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "该产品不存在");
        }

//
        Boolean b = productService.deleteProduct(id);
        return ResultUtils.success(b);

    }



    /**
     * 根据类别id、产品名称查询
     */
    @GetMapping()
    public BaseResponse<Page<ProductVo>> getProduct(ProductQueryRequest productQueryRequest) {

        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();

        if (productQueryRequest.getTypeid() != null && productQueryRequest.getTypeid() != 0) {
            queryWrapper.eq(Product::getTypeid, productQueryRequest.getTypeid());
        }

        if (StringUtils.isNotBlank(productQueryRequest.getName())) {
            queryWrapper.like(Product::getName, productQueryRequest.getName());
        }

        Page<Product> page = productService.page(new Page<>(productQueryRequest.getCurrent(),
                productQueryRequest.getPageSize()), queryWrapper);

        Page<ProductVo> productVoPage = new Page<>();
        BeanUtils.copyProperties(page, productVoPage);
        List<ProductVo> productVoList = page.getRecords().stream().map(product -> {
            ProductVo productVo = new ProductVo();
            BeanUtils.copyProperties(product, productVo);

            return productVo;
        }).collect(Collectors.toList());

//         处理图片
        String preurl = fileUtil.getPreurl();
        productVoList.stream().forEach(productVo -> {
            LambdaQueryWrapper<Resourse> resourseLambdaQueryWrapper = new LambdaQueryWrapper<>();
            resourseLambdaQueryWrapper.eq(Resourse::getProductid,productVo.getId());
            List<Resourse> list = resourseService.list(resourseLambdaQueryWrapper);
            List<String> urlList = list.stream().map(Resourse::getUrl).collect(Collectors.toList());
            if (urlList != null) {
                urlList = urlList.stream().map(item -> preurl + item).collect(Collectors.toList());
            }
            productVo.setProductUrlList(urlList);
        });

        productVoPage.setRecords(productVoList);

        return ResultUtils.success(productVoPage);
    }


}
